import { _decorator, Component, find, Node, tween, Animation, v3, Vec3, RigidBody2D, PhysicsSystem2D, v2 } from 'cc';
import { CarriageLayer } from './CarriageLayer';
import { CameraController } from './CameraController';
import { Hero } from './Hero';
const { ccclass, property } = _decorator;

const roofPos: Vec3[] = [v3(-27, 50), v3(-27, 230), v3(-27, 410), v3(-27, 590)];

const carriagerLayerPos: Vec3[] = [v3(0, 110), v3(0, 290), v3(0, 470)];

@ccclass('Chariot')
export class Chariot extends Component {
    private static _instance: Chariot = null;

    public static get Instance(): Chariot {
        if (!Chariot._instance) {
            Chariot._instance = new Chariot();
        }

        return Chariot._instance;
    }

    dirX: number = 10;


    @property(Node)
    roofNode: Node = null;

    @property(Node)
    carriageNode: Node = null;

    @property({ type: [Node] })
    CarriageLayer: Node[] = [];

    private _rigidBody: RigidBody2D = null;

    private _arrCarriageLayer: Array<CarriageLayer> = [null, null, null];
    private _curLayerCount: number = 0;

    private _wheel1: Node = null;
    private _wheel2: Node = null;
    private _animation1: Animation = null;
    private _animation2: Animation = null;

    protected onLoad(): void {
        Chariot._instance = this;
        this._rigidBody = this.node.getComponent(RigidBody2D);
        this.roofNode.setSiblingIndex(99);
        this._animation1 = find("前车轮", this.node).getChildByName("Image").getComponent(Animation);
        this._animation2 = find("后车轮", this.node).getChildByName("Image").getComponent(Animation);
    }

    protected start(): void {
        this.wheelRoll();
        this.updateRoofPos();
    }
    protected update(dt: number): void {
        this._rigidBody.linearVelocity = v2(this.dirX, 0);
    }

    createWeapon(type: number, layer: number) {
        if (!this.CarriageLayer[layer - 1].active) {
            this.CarriageLayer[layer - 1].active = true;
            this.createLayer(type, layer);
        }
        this._arrCarriageLayer[layer - 1].creatorWeapon();

    }


    createLayer(type: number, layer: number) {
        let carriage: Node = this.CarriageLayer[layer - 1];
        carriage.setPosition(carriagerLayerPos[this._curLayerCount++]);
        let ts: CarriageLayer = carriage.getComponent(CarriageLayer);
        ts.init(type);
        this._arrCarriageLayer[layer - 1] = ts;
        this.updateLayer();
        this.updateRoofPos();
    }


    updateRoofPos() {
        let targetPos = roofPos[this._curLayerCount];
        tween(this.roofNode)
            .to(0.1, { position: targetPos }, { easing: "linear" })
            .start();
    }

    updateLayer() {
        let curIndex = 0;
        for (let i = 0; i < this._arrCarriageLayer.length; i++) {
            if (this._arrCarriageLayer[i]) {
                let targtePos = carriagerLayerPos[curIndex++];
                tween(this._arrCarriageLayer[i].node)
                    .to(0.1, { position: targtePos }, { easing: `linear` })
                    .start();
            }
        }
    }

    removeLayer(carriage: CarriageLayer) {
        let index = this._arrCarriageLayer.findIndex(e => e === carriage);
        if (index == -1) {
            console.error("找不到该层！！");
            return;
        }
        this._arrCarriageLayer[index].node.active = false;
        this._arrCarriageLayer[index] = null;
        this._curLayerCount--;
        this.updateLayer();
        this.updateRoofPos()
    }

    beHit(harm: number) {
        for (let i = 0; i < this._arrCarriageLayer.length; i++) {
            if (this._arrCarriageLayer[i]) {
                this._arrCarriageLayer[i].beHit(harm)
                return;
            }
        }
        Hero.Instance.beHit(harm);
    }

    //轮子滚动
    wheelRoll() {
        this._animation1.play();
        this._animation2.play();
    }

    setWheelActive(bool: boolean) {
        this._wheel1.active = bool;
        this._wheel2.active = bool;
    }

}


